﻿
using System.Collections.Generic;

namespace UnityView
{
    public class BaseObjectPool<T>
    {
        public readonly List<T> Objects = new List<T>();

        public delegate T Instantiate();

        public interface IBaseUpdater<TK>
        {
            TK Instantiate();
            void EnqueueUpdate(TK t);
            void DequeueUpdate(TK t);
        }

        protected readonly IBaseUpdater<T> Updater;
        public BaseObjectPool(IBaseUpdater<T> updater)
        {
            Updater = updater;
        } 
        public T Dequeue()
        {
            T t;
            if (Objects.Count > 0)
            {
                t = Objects[0];
                Objects.RemoveAt(0);
            }
            else
            {
                t = Updater.Instantiate();
            }
            Updater.DequeueUpdate(t);
            return t;
        }

        public void Enqueue(T t)
        {
            Updater.EnqueueUpdate(t);
            Objects.Add(t);
        }
    }
}
